/* user.c : User processes
 */

#include <kernel.h>

/* Your code goes here */
/* Producer prints SELLER 12 times. */
void producer(){
	int loopIteration = 0;
	while(TRUE){
		if( loopIteration < 12){
			verbose("SELLER process executing. My PID is %d. Printing with sysputs:", sysgetpid());
			sysputs("'HELLO FROM SELLER'\n");
			sysyield();
		}
		else{
			sysputs("SELLER process done. Cleaning up.\n");
			sysstop();
		}
		loopIteration++;
	}
}

/* Consumer prints BUYER 15 times */
void consumer(){
	int loopIteration = 0;
	while(TRUE){
		if( loopIteration < 15){
			verbose("BUYER process executing. My PID is %d. Printing with sysputs:", sysgetpid());
			sysputs("'HELLO FROM BUYER'\n");
			sysyield();
		}
		else{
			sysputs("BUYER process done. Cleaning up.\n");
			sysstop();
		}
		loopIteration++;
	}
}

void idleproc(){
	while (TRUE){
		if (PREEMPTION_UNIT_TEST) kprintf("IDLE\n");
		asm volatile("hlt\n");
	}
}

/* Root is the first process loaded. Any later processes must be loaded by root or a child of root. */
void root (){
	verbose("Root A\n");
	sysyield();
	verbose("Root B\n");
	sysyield();
	verbose("Root C\n");
	sysyield();
	verbose("Root D\n");	
	
	verbose("\n Root process created. System started.\n");
	/* Spawn user processes */
	verbose(" root process spawing Producer() process.\n");
	syscreateN(producer, 1024, "Producer"); 
	verbose(" root process spawing Consumer() process.\n");
	syscreateN(consumer, 1024, "Consumer");
	
	while (TRUE){
		verbose("Root PID is %d.\n", sysgetpid());
		sysyield();		
	}
}
